#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct Node
{
    ElemType data;
    Node* next;
}Node, *Link;
typedef struct
{
    Link top;
    Link base;
    int length;
}LinkStack;
Status InitLinkStack(LinkStack &LS)
{
    Link tail = (Link)malloc(sizeof(Node));
    if (!tail)
        exit(OVERFLOW);
    tail->next = NULL;
    LS.base = LS.top = tail;
    LS.length = 0;
    return OK;
}
Status Push(LinkStack &LS, ElemType e)
{
    Link node = (Link)malloc(sizeof(Node));
    if (!node)
        exit(OVERFLOW);
    node->data = e;
    node->next = LS.top;
    LS.top = node;
    ++LS.length;
    return OK;
}
Status Pop(LinkStack &LS, ElemType &e)
{
    if (LS.base == LS.top)
        return ERROR;
    Link cur = LS.top;
    LS.top = cur->next;
    e = cur->data;
    free(cur);
    cur = NULL;
    --LS.length;
    return OK;
}
Status GetTop(LinkStack LS, ElemType &e)
{
    if (LS.base == LS.top)
        return ERROR;
    e = LS.top->data;
    return OK;
}
Status showStack(LinkStack LS)
{
    printf("total: %d\n", LS.length);
    Link p = LS.top;
    for (int i = 0; i < LS.length; ++i, p = p->next)
        printf("%d ", p->data);
    printf("\n");        
    return OK;
}
int main(void)
{
    LinkStack ls;
    InitLinkStack(ls);
    showStack(ls);
    ElemType e = 5;
    Push(ls, e);
    e = 6;
    Push(ls, e);
    showStack(ls);
    e = 4;
    Pop(ls, e);
    printf("e = %d\n", e);
    showStack(ls);
    GetTop(ls, e);
    printf("e = %d\n", e);

    return 0;
}